package com.wq.httpclientcore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wq.httpclientcore.convert.Convertor;
import com.wq.httpclientcore.entities.po.AuthConfig;
import com.wq.httpclientcore.entities.req.query.AuthConfigREQ;
import com.wq.httpclientcore.entities.req.save.AuthConfigSaveREQ;
import com.wq.httpclientcore.entities.vo.AuthConfigVO;
import com.wq.httpclientcore.model.Result;
import com.wq.httpclientcore.service.AuthConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import java.util.*;

/**
 * @author wangqi
 * @since 2025-03-13 10:46:59
 */
@Tag(name = "鉴权配置")
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/module/authConfig")
public class AuthConfigController {

    private final AuthConfigService authConfigService;

    @Operation(summary = "查询列表")
    @Parameters({@Parameter(name = "pageNo", hidden = true), @Parameter(name = "pageSize", hidden = true)})
    @GetMapping("/list")
    public Result<List<AuthConfigVO>> list(@ParameterObject AuthConfigREQ req) {
        List<AuthConfig> poList = authConfigService.list(req);
        return Result.success(Convertor.INSTANCE.convertAuthConfigs(poList));
    }

    @Operation(summary = "分页查询")
    @GetMapping("/page")
    public Result<IPage<AuthConfigVO>> page(@ParameterObject AuthConfigREQ req) {
        Page<AuthConfig> page = authConfigService.pageList(req);
        IPage<AuthConfigVO> result = page.convert(Convertor.INSTANCE::convert);
        return Result.success(result);
    }

    @Operation(summary = "查询详情")
    @GetMapping("/{id}")
    public Result<AuthConfigVO> getById(@PathVariable("id") Long id) {
        AuthConfig data = authConfigService.getById(id);
        if (null == data) {
            return Result.fail("数据不存在或已删除");
        }

        return Result.success(Convertor.INSTANCE.convert(data));
    }

    @Operation(summary = "新增")
    @PostMapping
    public Result<?> add(@RequestBody AuthConfigSaveREQ req) {
        AuthConfig po = Convertor.INSTANCE.convert(req);
        return Result.success(authConfigService.saveAuthConfig(po));
    }

    @Operation(summary = "修改")
    @PutMapping("/{id}")
    public Result<?> edit(@PathVariable("id") Long id, @RequestBody AuthConfigSaveREQ req) {
        AuthConfig po = Convertor.INSTANCE.convert(req);
        po.setId(id);
        return Result.success(authConfigService.updateAuthConfig(po));
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{ids}")
    public Result<?> delete(@PathVariable Long[] ids) {
        authConfigService.deletedById(Arrays.asList(ids));
        return Result.success("删除成功");
    }

}
